define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'project/index' + location.search,
                    add_url: 'project/add',
                    edit_url: 'project/edit',
                    del_url: 'project/del',
                    multi_url: 'project/multi',
                    table: 'project',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'),operate:'LIKE'},
                        {field: 'province.Name', title: __('Province_id')},
                        {field: 'city.Name', title: __('City_id')},
                        {field: 'area.Name', title: __('Area_id')},
                        {field: 'need_money', title: __('需求总金额')},
                        // {field: 'qian_money', title: __('签收总金额')},
                        {field: 'address', title: __('Address')},
                        {field: 'admin.nickname', title: __('Admin_id')},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'switch', title: __('Switch'), table: table, formatter: Table.api.formatter.toggle},
                        {field: 'status', title: __('Status')},
                        {field: 'addtime', title: __('Addtime'), operate:'RANGE', addclass:'datetimerange'},
                        // {field: 'img', title: __('Img')},
                        {
                            field: 'operate', title: __('Operate'), 
                            table: table, 
                            events: Table.api.events.operate, 
                            formatter: Table.api.formatter.operate,
                            buttons:[
                               {name: '需求单', text: '需求单', title: '需求单', classname: 'btn btn-xs btn-primary btn-addtabs', url: 'order/index'},
                               {name: '签收单', text: '签收单', title: '签收单', classname: 'btn btn-xs btn-danger btn-addtabs', url: 'qian/index'},
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(document).on("click", ".btn-append", function(){
                Form.events.plupload("#add-form");
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                 $("#c-city").data("params", function (obj) {
                    return {'provice_id': $("#c-provice").val()};
                });

                $("#c-area").data("params", function (obj) {
                    return {'city_id': $("#c-city").val()};
                });

                $("#c-tag").data("params", function (obj) {
                    return {'provice_id': $("#c-provice").val(),'city_id': $("#c-city").val()};
                });
                $("#c-pid").data("params", function (obj) {
                    return {'provice_id': $("#c-provice").val(),'city_id': $("#c-city").val(),'tag':$('#c-tag').val()};
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});